package com.blb.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.community.Annotation.Log;
import com.blb.community.entity.SysLogininfor;
import com.blb.community.service.ISysLogininforService;
import com.blb.community.utils.EasyExcelUtil;
import com.blb.community.utils.ResponseResult;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统访问记录 前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Api(tags = "登录日志管理模块")
@RestController
@RequestMapping("/sys-logininfor")
public class SysLogininforController {

    @Autowired
    private ISysLogininforService iSysLogininforService;

    /**
     * 分页查询登录日志列表
     * @return
     */
    @ApiOperation("查询登录日志")
    @GetMapping("/Logininfor")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
    public ResponseResult<IPage<SysLogininfor>> getAllLogininfor(Long current){
        IPage<SysLogininfor> sysLogininforIPage = iSysLogininforService.getAllLogininfor(current, 5L);
        return ResponseResult.ok(sysLogininforIPage);
    }

    /**
     * 逻辑删除登录日志
     * @param idList
     * @return
     */
    @Log(module = "删除登录日志",operationType =3,requestMethod = "DELETE")
    @ApiOperation("删除登录日志")
    @DeleteMapping("/deleteLogininfor/{idList}")
    public ResponseResult<String> deleteLogininfor(@PathVariable ArrayList<String> idList){
       iSysLogininforService.removeByIds(idList);
        return ResponseResult.ok("删除成功");
    }

    @ApiOperation("登录日志详情")
    @GetMapping("/logininforDetails/{id}")
    public ResponseResult<List<SysLogininfor>> selectAllOperLog(@PathVariable Long id){
        return ResponseResult.ok(iSysLogininforService.selectLogininfor(id));
    }

    @ApiOperation("登录日志模糊查询")
    @GetMapping("/fuzzyLogininfor")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
    public ResponseResult<IPage<SysLogininfor>> getFuzzyOperLog(Long current,String userName, String ipaddr, String status, String beginTime, String endTime){
        IPage<SysLogininfor> logininfor = iSysLogininforService.getLogininfor(current, 5L, userName, ipaddr, status, beginTime, endTime);
        return ResponseResult.ok(iSysLogininforService.getLogininfor(current, 5L,userName,ipaddr,status,beginTime,endTime));
    }


    @ApiOperation("表格导出")
    @PostMapping("/excel")
    public void downloadExcel(@RequestBody List<SysLogininfor> list, HttpServletResponse response){
        EasyExcelUtil.write(list,SysLogininfor.class,response);
    }

}
